import { Text, View, Button, Image,Input } from "@tarojs/components";

import { useEffect, useRef, useState } from "react";
import { getUserInfo, selectUserInfo, updateUser, updateUserInfo } from "../../../api/user";
import Taro from "@tarojs/taro";
import { Video } from "@nutui/nutui-react-taro";
import { getStore } from "../../../utils/localStore";
import { selectEnts } from "../../../api/ceshi";
import { iconRightGray, yxxf } from "../../../assets/files";
import { baseUrl } from "../../../utils/request";
import "./index.scss";

definePageConfig({
  navigationBarTitleText: "昵称",
});

export default function EditUserInfo() {
  const [userName, setUserName] = useState("");
  const [useData, setUseData] = useState({});
  const [headId, setHeadId] = useState("");

  const [enterpriseName, setEnterpriseName] = useState("");

  useEffect(() => {
    getUserInfo()
  }, []);

  async function getUserInfo() {
    const { data } = await selectUserInfo({})
    setUserName(data.userName)
    setHeadId(data.headId)
    setEnterpriseName(data.enterpriseName)
  }

  const saveUserInfo = async () => {

    console.log(userName);
    console.log(nicknameInputRef.current);

    if (!userName) {
      await Taro.showToast({
        icon: "none",
        title: "请输入昵称！",
        duration: 2000,
      });
      return;
    }

    const result = await updateUser({
      userName,
    });

    if (!result?.success) {
      await Taro.showToast({
        icon: "none",
        title: result?.msg || "请求失败！",
        duration: 2000,
      });
      return;
    }
    await Taro.navigateBack();
  };

  const chooseAvatar = (e) =>{
    setHeadId(e?.detail?.avatarUrl)
    console.log(e?.detail?.avatarUrl);
  }

  const nicknameInputRef = useRef(null);

  return (
    <View className={"improve-user-info-page"}>


      <View className={"input-wrapper"}>
        <Text className={"input-title"}>昵称:</Text>
        {/* <Input
          type="nickname"
          className="input-class"
          value={userName}
          ref={nicknameInputRef}
          onInput={(value) => {
            console.log(value?.detail?.value);
            setUserName(value?.detail?.value || "")
          }}
          placeholder={userName || "请输入昵称"}
        /> */}

<Input
	type="nickname" 
	className="input-class"
	placeholder="请输入昵称"
  maxlength={16}
	ref={nicknameInputRef}
  value={userName}
	onInput={(e) => {
    console.log("oninput");
    console.log(e.detail.value);
    setUserName(e.detail.value)
  }}
/>

      </View>

      <Button
        className={"save-btn"}
        onClick={saveUserInfo}
      >
        保存
      </Button>

    </View>
  );
}
